<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="demo-input-suffix gl_searchBox">
      <el-row :gutter="20">
        <el-col :md="4">
          <p>课程名称</p>
          <el-input v-model="selectedMsg.Filter" placeholder="请输入课程名称" size="mini" clearable @change="getPageList()" />
        </el-col>
        <el-col :md="4">
          <p>授课模式</p>
          <el-select v-model="selectedMsg.TeachingModeCode" size="mini" placeholder="请选择授课模式" filterable @change="getPageList()">
            <el-option label="全部" value="" />
            <el-option v-for="item in dict.teachingMode" :key="item.code" :value="item.code" :label="item.value" />
          </el-select>
        </el-col>
        <el-col :md="16" class="search_btn">
          <!-- 查询按钮 -->
          <el-button type="primary" size="mini" @click="getPageList()">筛选</el-button>
          <!-- 重置按钮 -->
          <el-button type="warning" size="mini" @click="clickReset()">{{ $t('button.Reset') }}</el-button>
        </el-col>
      </el-row>
    </div>
    <template>
      <el-row class="gl_my_15">
        <el-col :md="6" :lg="4">
          <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="addJump()">新建课程</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="tableLoading" :data="tableData" class="gl_table_noBorder" size="mini" style="width: 100%;">
        <el-table-column label="序号" type="index" width="70" :index="table_index" />
        <el-table-column label="课程名称" prop="name" />
        <el-table-column label="授课模式">
          <template slot-scope="scope">
            {{ scope.row.teachingMode.name || '' }}
          </template>
        </el-table-column>
        <el-table-column label="收费模式">
          <template slot-scope="scope">
            <span v-if="scope.row.lessonStandardCode === 'Period'">{{ scope.row.amount + '课时 / ' + scope.row.lessonPeriod.name }}</span>
            <span v-else>{{ scope.row.amount + '课时' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开班数">
          <template slot-scope="{row}">
            {{ row.classCount + '个' }}
          </template>
        </el-table-column>
        <el-table-column label="排课数">
          <template slot-scope="{row}">
            {{ row.courseScheduleCount }}
          </template>
        </el-table-column>
        <el-table-column label="未上课数">
          <template slot-scope="{row}">
            {{ row.unFinishedCourseScheduleCount }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" :min-width="130">
          <template slot-scope="{row}">
            <span v-if="row.creationTime">
              {{ row.creationTime | FormatDate('yyyy-MM-dd HH:mm:ss') }}
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('table.actions')" width="160">
          <template slot-scope="{ row }">
            <el-button size="mini" type="text" @click="clickEdit(row)">编辑</el-button>
            <el-button size="mini" type="text" style="color:#FF3434" @click="clickDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="gl_pt_15" style="text-align: right;">
        <el-pagination
          :current-page="selectedMsg.Page"
          :page-size="selectedMsg.MaxResultCount"
          :total="total"
          layout="total,sizes,prev,pager,next,jumper"
          :page-sizes="[10, 20, 30, 40]"
          @size-change="sizeChange"
          @current-change="pageIndexChange"
        />
      </el-row>
    </template>
    <new-course ref="newCourse" />
  </div>
</template>

<script>
import { deepCopy } from '@/lib';
import newCourse from './newCourse';
import { mapState, mapActions } from 'vuex';
import { Api_GET_ClubCourseQO } from '@/lib/form/hall/swaggerForm';
import { api_GET_ClubCourse, api_DELETE_ClubCourseId } from '@/extend/api/hallswaggerApi/app';
export default {
  components: { newCourse },
  data() {
    return {
      selectedMsg: new Api_GET_ClubCourseQO(),
      typeList: [],
      tableLoading: false,
      total: 0,
      tableData: [],
      storeId: ''
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict,
      cache: state => state.dict.cache
    })
  },
  mounted() {
    if (this.selectedMsg) {
      this.selectedMsg.TeachingModeCode = '';
    }
    this.getDict();
    this.getPageList();
  },
  methods: {
    // 若没有字典信息，外部可直接调用方法获取字典数据
    ...mapActions({ getDict: 'dict/getDict', getDictByType: 'dict/getDictByType' }),
    // 获取课程列表
    getPageList(page) {
      this.selectedMsg.Page = page || 1;
      this.selectedMsg.SkipCount = (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount;
      this.selectedMsg.IncludeTeachingMode = true;
      this.selectedMsg.IncludeLessonPeriod = true;
      this.tableLoading = true;
      api_GET_ClubCourse(this.selectedMsg).then(res => {
        this.tableLoading = false;
        this.tableData = res.items;
        this.total = res.totalCount;
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    clickEdit(row) {
      this.$refs.newCourse.title = '编辑课程';
      this.$refs.newCourse.dialogVisible = true;
      this.$refs.newCourse.id = row.id;
      this.$refs.newCourse.courseForm = deepCopy(row);
      for (let i = 0; i < this.$refs.newCourse.radioList.length; i++) {
        if (this.$refs.newCourse.radioList[i] === this.$refs.newCourse.courseForm.courseScheduleColor) {
          this.$refs.newCourse.colorIndex = i;
          return;
        }
      }
    },
    clickDelete(row) {
      const that = this;
      that.$confirm(`您确定要删除吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true;
          api_DELETE_ClubCourseId(row.id).then(res => {
            this.loading = false;
            that.$message({ type: 'success', message: '删除成功' });
            that.getPageList();
          }, error => {
            this.loading = false;
            const _msg = error.error.message || error.message;
            that.$message({ type: 'error', message: _msg });
          });
        })
        .catch(() => {
        });
    },
    clickReset() {
      this.selectedMsg = new Api_GET_ClubCourseQO();
      this.selectedMsg.TeachingModeCode = '';
      this.getPageList();
    },
    addJump() {
      this.$refs.newCourse.title = '新建课程';
      this.$refs.newCourse.dialogVisible = true;
      this.$refs.newCourse.id = '';
    },
    table_index(index) {
      return (this.selectedMsg.Page - 1) * this.selectedMsg.MaxResultCount + index + 1;
    },
    // 分页
    sizeChange(MaxResultCount) {
      this.selectedMsg.MaxResultCount = MaxResultCount;
      this.getPageList();
    },
    pageIndexChange(currentPage) {
      this.getPageList(currentPage);
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
